package com.heima.media.listen;

import com.heima.media.entity.WmNews;
import com.heima.media.service.IAuditService;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class PublishListen {
    @Autowired
    private RedissonClient client;
    @Autowired
    private IAuditService auditService;
    @PostConstruct//实例化后类自动执行该方法
    public void init(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                //获得一个阻塞队列
                RBlockingDeque<Object> blockingDeque = client.getBlockingDeque("article-publish");
                RDelayedQueue<Object> delayedQueue = client.getDelayedQueue(blockingDeque);
                delayedQueue.offer("init",1, TimeUnit.SECONDS);
                while (true) {
                    try {
                        Object msg = blockingDeque.poll(1, TimeUnit.MINUTES);
                        if(msg!=null&&!msg.equals("init")){
                            System.out.println("接收到消息"+msg+ new Date());
                            // 重新发布文章
                            auditService.aduit((WmNews) msg);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }



}
